/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.user.client;

import com.cleangwt.user.client.ActionEvent.ActionHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.HasHandlers;

/**
 * Represents an action event.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class ActionEvent extends GwtEvent<ActionHandler> {
  /**
   * Handler interface for {@link ActionEvent} events.
   */
  public interface ActionHandler extends EventHandler {
    /**
     * Called when a {@link ActionEvent} is fired.
     * 
     * @param event the {@link ActionEvent} that was fired
     */
    void onAction(ActionEvent event);
  }

  /**
   * Interface specifying that a class can add {@code ActionHandler}s.
   */
  public interface HasActionHandlers extends HasHandlers {
    /**
     * Adds a {@link ActionEvent} handler.
     * 
     * @param handler the handler
     * @return {@link HandlerRegistration} used to remove this handler
     */
    HandlerRegistration addActionHandler(ActionHandler handler);
  }

  /**
   * Handler type.
   */
  private static Type<ActionHandler> TYPE;

  /**
   * Fires an action event on all registered handlers in the handler manager. If no such handlers
   * exist or no specified process for the action, it will do nothing.
   * 
   * @param source the source
   * @param event the action action
   */
  public static <T> void fire(HasActionHandlers source, ActionEvent event) {
    if (TYPE != null) {
      event.setSource(source);
      source.fireEvent(event);
    }
  }

  /**
   * Fires an action event on all registered handlers in the handler manager. If no such handlers
   * exist or no specified process for the action, it will do nothing.
   * 
   * @param source the source of the handlers
   * @param action the action
   */
  public static <T> void fire(HasActionHandlers source, String action) {
    fire(source, new ActionEvent(action));
  }

  /**
   * Gets the type associated with this event.
   * 
   * @return returns the handler type
   */
  public static Type<ActionHandler> getType() {
    if (TYPE == null) {
      TYPE = new Type<ActionHandler>();
    }
    return TYPE;
  }

  private final String action;

  /**
   * Creates a action event.
   * 
   * @param action
   */
  private ActionEvent(String action) {
    assert action != null;
    this.action = action;
  }

  /**
   * @return the action
   */
  public String getAction() {
    return action;
  }

  /**
   * Gets the type associated with this event.
   * 
   * @return returns the handler type
   */
  @Override
  public Type<ActionHandler> getAssociatedType() {
    return ActionEvent.TYPE;
  }

  @Override
  protected void dispatch(ActionHandler handler) {
    handler.onAction(this);
  }

}
